﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TankServer.Core;

namespace TankServer.Logic
{
    class HandleConnMsg
    {
        public void MsgHeatBeat(Conn conn,ProtocolBase protoBase)
        {
            conn.lastTickTime = Sys.GetTimeStamp();
            //Console.WriteLine("更新心跳时间"+conn.GetAddress());
        }
        
        //注册
        //参数 str 用户名 pw 密码
        //返回协议 -1 失败 0 成功
        public void MsgRegister(Conn conn,ProtocolBase protoBase)
        {
            int start = 0;
            ProtocolBytes protocol = (ProtocolBytes)protoBase;
            string protoName = protocol.GetString(start,ref start);
            string id = protocol.GetString(start,ref start);
            string pw = protocol.GetString(start,ref start);
            string strFormat = "[收到注册协议]" + conn.GetAddress();
            Console.WriteLine(strFormat+"用户名"+id+"密码"+pw);
            protocol = new ProtocolBytes();
            protocol.AddString("Register");
            if (DataMgr.instance.Register(id, pw))
            {
                protocol.AddInt(0);
            }
            else
            {
                protocol.AddInt(-1);
            }
            DataMgr.instance.CreatePlayer(id);
            conn.Send(protocol);
        }

        //登录
        public void MsgLogin(Conn conn,ProtocolBase protoBase)
        {
            int start = 0;
            ProtocolBytes protocol = (ProtocolBytes)protoBase;
            string protoName = protocol.GetString(start,ref start);
            string id = protocol.GetString(start,ref start);
            string pw = protocol.GetString(start,ref start);
            string strFormat = "[收到登录协议]" + conn.GetAddress();
            Console.WriteLine(strFormat+"用户名"+id+"密码"+pw);

            ProtocolBytes protocolRet = new ProtocolBytes();
            protocolRet.AddString("Login");
            if (!DataMgr.instance.CheckPassWord(id, pw))
            {
                Console.WriteLine("CheckPassWord Failed...");
                protocolRet.AddInt(-1);
                conn.Send(protocolRet);
                return;
            }
            //是否已经登录
            ProtocolBytes protocolLogout = new ProtocolBytes();
            protocolLogout.AddString("Logout");
            if (!Player.KickOff(id, protocolLogout))
            {
                protocolRet.AddInt(-1);
                conn.Send(protocolRet);
            }
            PlayerData playerData = DataMgr.instance.GetPlayerData(id);
            if(playerData == null)
            {
                protocolRet.AddInt(-1);
                conn.Send(protocolRet);
                return;
            }
            conn.player = new Player(id,conn);
            conn.player.data = playerData;
            ServNet.instance.handlePlayerEvent.OnLogin(conn.player);

            protocolRet.AddInt(0);
            conn.Send(protocolRet);
            return;
        }

        public void MsgLogout(Conn conn,ProtocolBase protoBase)
        {
            ProtocolBytes protocol = new ProtocolBytes();
            protocol.AddString("Logout");
            protocol.AddInt(0);
            if(conn.player == null)
            {
                conn.Send(protocol);
                conn.Close();
            }
            else
            {
                conn.Send(protocol);
                conn.player.Logout();
            }
        }
           
    }
}
